package service.inventory.impl;

import dao.inventory.ItemPriceDao;
import java.util.Date;
import java.util.List;
import java.util.Map;
import model.inventory.ItemPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.inventory.ItemPriceService;
import utility.NameValueList;
import utility.StringUtils;


@Service("itemPriceService")
@Transactional(readOnly=true)
public class ItemPriceServiceImpl implements ItemPriceService {

    //void LazyInitializationException 
    String mainQuery = "select distinct itemPrice from ItemPrice itemPrice left join fetch itemPrice.item item "+
            "left join fetch itemPrice.unit unit ";
    
    @Autowired private ItemPriceDao itemPriceDao;
    
    @Transactional(readOnly=false)
    @Override
    public boolean save(List<ItemPrice> itemPrice) {
        return itemPriceDao.saveData(itemPrice);
    }

    @Transactional(readOnly=false)
    @Override
    public boolean delete(ItemPrice itemPrice) {
        return itemPriceDao.delete(itemPrice);
    }

    @Override
    public ItemPrice get(int itemId, int priceLevel, Date curdate) {
        String query=mainQuery+" where itemPrice.id.itemId = "+itemId+
               (priceLevel>0 ? " and itemPrice.id.priceLevel = "+priceLevel:"")+
               (curdate!=null ? " and itemPrice.id.startDate = "+StringUtils.formatSqlDate(curdate):"");
        return itemPriceDao.getByCustomQuery(query);
    }

    @Override
    public List<ItemPrice> getAll(Map<NameValueList, Object> params) {
        String query=mainQuery+StringUtils.getFilter(params);
        return itemPriceDao.getAll(query);
    }

    @Override
    public List<ItemPrice> getAll(int start, int num, Map<NameValueList, Object> params) {
        String query=mainQuery+StringUtils.getFilter(params);
        return itemPriceDao.getAll(start, num, query);
    }

    @Override
    public List<Date> getDates(int itemId) {
        return itemPriceDao.getDates(itemId);
    }
    
}
